import qb_database
import json
import time
import datetime
import re
import config
import numpy
import qb


# Todo remove dying job
def re_dying_job():
    q = qb_database.QubeDatabase()
    job_dying = q.job(query_field='id,status,todo,name', query='status = 261 or todo = 0')
    for i in job_dying:
        print i['id'], i['status'],i['name'],i['todo']
        print qb.remove(i['id'])


def pasted_time(t):
    if t == '':
        return ''
    s = t % 60
    m = t / 60 % 60
    h = t / 3600
    return '%02d:%02d:%02d' % (h, m, s)


def get_qube_time_utc(qube_time):
    stamp_qube = int(qube_time) + config.QUBE_TIME
    get_the_time_utc = datetime.datetime.utcfromtimestamp(stamp_qube)
    data_time = get_the_time_utc.strftime("%Y-%m-%d")
    return data_time


def get_stamp_qube(number_of_day):
    time_start = (datetime.datetime.now() + datetime.timedelta(days=number_of_day)).strftime("%Y-%m-%d %H:%M:%S")
    timestart_arr_start = time.strptime(time_start, "%Y-%m-%d %H:%M:%S")
    time_start_local = time.localtime(time.mktime(timestart_arr_start))
    timestart_qube_start = int(time.mktime(time_start_local)) - config.QUBE_TIME
    return timestart_qube_start


def data_get_seq_time_type_pie(file_path):
    q = qb_database.QubeDatabase()
    dict_type_submit = {}
    dict_sequence = {}
    dict_sequence_2 = {}
    all_status_pie = []
    count_all_status = []
    dict_status_all = {'complete': 0, 'pending': 0, 'failed': 0, 'run': 0, 'block': 0}
    dict_time = {}
    timestart_arr_end = time.time()
    timestart_qube_end = int(timestart_arr_end) - config.QUBE_TIME
    job_info = q.job(query_field='id,name,prod_shot,prod_seq,todotally,todo,timesubmit,timestart,status',
                     query='(status != 261) and (status != 48) and (status != 265) and (timesubmit >= %s and timesubmit <= %s)'%(get_stamp_qube(-3),timestart_qube_end))

    if file_path == 'night':
        day = 0
    else:
        day = -1
    for job in job_info:
        # get type of submit subjob
        if get_qube_time_utc(job['timesubmit']) == get_qube_time_utc(get_stamp_qube(day)):
            ret = re.match(r'[0-9]+|[A-Z{1}][A-Za-z]+', job["name"])
            if ret:
                type_name = ret.group()
                if type_name.isdigit():
                    type_name = 'OUTPUT_COMP'
                if not dict_type_submit.has_key(type_name):
                    dict_type_submit[type_name] = 0
                dict_type_submit[type_name] += int(job['todo'])
        # get the seq subjob status
        if (get_qube_time_utc(job['timesubmit']) == get_qube_time_utc(get_stamp_qube(day))) or (job['status'] == 320 or job['status'] == 336):
            sequence = job['prod_seq'].split('_')[0]  # SQ0010 is lgt
            status = map(int, job['todotally'].split(' '))
            all_status_pie.append(status)
            count_all_status = list(numpy.sum(all_status_pie,axis=0))
            if not dict_sequence.has_key(sequence):
                dict_sequence[sequence] = []
            dict_sequence[sequence].append(status)

        # get the time use
        subjob = q.subjob(int(job["id"]), query_field='id,status,timestart,timecomplete',
                          query='status = 16')
        for sub in subjob:
            if get_qube_time_utc(sub['timestart']) == get_qube_time_utc(get_stamp_qube(day)):
                time_start = datetime.datetime.fromtimestamp(sub["timestart"] + config.QUBE_TIME)
                time_end = datetime.datetime.fromtimestamp(sub["timecomplete"] + config.QUBE_TIME)
                time_use = (time_end - time_start).total_seconds()
                time_change_hour = pasted_time(time_use)
                time_list = time_change_hour.split(':')
                if int(time_list[1]) >= 30:
                    time_list[0] = int(time_list[0]) + 1
                if not dict_time.has_key(int(time_list[0])):
                    dict_time[int(time_list[0])] = 0
                dict_time[int(time_list[0])] += 1

    # get the all status dict
    dict_status_all['complete'] = count_all_status[2]
    dict_status_all['pending'] = count_all_status[3]
    dict_status_all['failed'] = count_all_status[5]
    dict_status_all['run'] = count_all_status[0]
    dict_status_all['block'] = count_all_status[1]

    # get the seq subjob status dict
    for key,value in dict_sequence.items():
        dict_sequence[key] = list(numpy.sum(dict_sequence[key], axis=0))
        if not dict_sequence_2.has_key(key):
            dict_sequence_2[key] = {'complete': 0, 'pending': 0, 'failed': 0, 'run': 0, 'block': 0}
        dict_sequence_2[key]['complete'] = dict_sequence[key][2]
        dict_sequence_2[key]['pending'] = dict_sequence[key][3]
        dict_sequence_2[key]['failed'] = dict_sequence[key][5]
        dict_sequence_2[key]['run'] = dict_sequence[key][0]
        dict_sequence_2[key]['block'] = dict_sequence[key][1]
    if file_path == 'morning':
        file_path1 = '/show/WDN/misc/dept/lgt/RenderFarm/archive/Classification_report/daily_report/data_json/'
    elif file_path == 'night':
        file_path1 = '/show/WDN/misc/dept/lgt/RenderFarm/archive/Classification_report/daily_report/data_json/night/'

    with open(file_path1+'%s_type_submit.json' % time.strftime("%Y-%m-%d", time.localtime(time.time())), 'w') as f:
        jason_strr_type = json.dumps(dict_type_submit, indent=4)
        f.write(jason_strr_type)
    print 'type ok'
    with open(file_path1 + '%s_seq.json' % time.strftime("%Y-%m-%d", time.localtime(time.time())), 'w') as f:
        jason_strr_seq = json.dumps(dict_sequence_2, indent=4)
        f.write(jason_strr_seq)
    print 'seq ok'
    with open(file_path1+'%s_status_all.json' % time.strftime("%Y-%m-%d", time.localtime(time.time())), 'w') as f:
        jason_strr_status = json.dumps(dict_status_all, indent=4)
        f.write(jason_strr_status)
    print 'pie ok'
    with open(file_path1 + '%s_time.json' % time.strftime("%Y-%m-%d", time.localtime(time.time())), 'w') as f:


        jason_strr_time = json.dumps(dict_time, indent=4)
        f.write(jason_strr_time)
    print 'timeuse ok'

if __name__ == "__main__":
    # data_get_seq_time_type_pie('morning')
    # print 'ok'
    while True:
        current_time = time.localtime(time.time())
        if ((current_time.tm_hour == 9) and (current_time.tm_min == 0) and (current_time.tm_sec == 0)):  #((current_time.tm_hour == 13) and (current_time.tm_min == 53) and (current_time.tm_sec == 0))
            t1=datetime.datetime.now()
            try:
                data_get_seq_time_type_pie('morning')
            except:
                print "some job dying"
                re_dying_job()
                data_get_seq_time_type_pie('morning')
        if ((current_time.tm_hour == 18) and (current_time.tm_min == 30) and (
            current_time.tm_sec == 0)):
            t1 = datetime.datetime.now()
            print 'try to get date'
            try:
                data_get_seq_time_type_pie('night')
            except:
                print "some job dying"
                re_dying_job()
                data_get_seq_time_type_pie('night')
            t2 = datetime.datetime.now()
            print t2-t1
            print 'json_record is ok!!!!'
        time.sleep(1)